package com.hr.hrto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hr.hrto.common.CustomException;
import com.hr.hrto.domain.Dish;
import com.hr.hrto.domain.DishFlavor;
import com.hr.hrto.domain.Setmeal;
import com.hr.hrto.domain.SetmealDish;
import com.hr.hrto.dto.DishDto;
import com.hr.hrto.mapper.DishMapper;
import com.hr.hrto.mapper.SetmealMapper;
import com.hr.hrto.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 黄瑞
 * @Date: 2022/07/07/13:32
 * @Description:
 * @version:1.0
 */
@Slf4j
@Service

@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetMealDishService setMealDishService;
    //新增菜品并保存口味
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //基本信息保存到dish表
        this.save(dishDto);
        Long id = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map((item)->{
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());
        //保存口味flavor表
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询对应菜品信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        DishDto dishDto =new DishDto();
        //查询基本信息
        Dish dish = this.getById(id);
        BeanUtils.copyProperties(dish,dishDto);
        //查询口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);


        return dishDto;
    }

    @Override
    @Transactional
    public void updteWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //清理当前菜品的口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //添加当前提交过来的数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存口味flavor表
        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public void removeWithStatus(List<Long> ids) {
        //查询套餐状态，判断是否可以删除
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getDishId,ids);
        //如果不能删除，抛业务异常
        int count1 = this.count(queryWrapper);
        if (count1>0){
            throw new CustomException("菜品正在售卖中，不能删除");
        }
        int count2 = setMealDishService.count(lambdaQueryWrapper);
        if (count2>0){
            throw new CustomException("菜品已与套餐绑定，请删除套餐后重试");
        }
        //删除套餐
        this.removeByIds(ids);


        //setMealDishService.remove(lambdaQueryWrapper);
    }



}
